require(['common'], function (Swiper) {
  "use strict";

  // 新增模拟页面
  $(".showMaterial").on("click",function(){
    simulateMaterials();
  })


  // 签证材料查看
  function simulateMaterials(){
    var simulateData = { list: ["在职人员","学龄前儿童","自由职业者","退休人员","青年","家庭主妇"] };
    // 定义模板
    var siumulateContent = _.template($("#simulate-visa-materials").html())(simulateData);
    var siumulateFooter = '<footer class="gzl-footer">'+
    '            <div class="gzl-footbar">'+
    '              <div class="gzl-flex__item">选择资料发送至邮箱</div>'+
    '            </div>'+
    '          </footer> '
    // 模板新绑定事件;
    function templateEvents(){
      // self 从_other中抽取; 勿乱修改
      $(".visaMaterials").find("section").on("click", ".gzl-cell_spread", function () {
        var con = $(this).parent().find(".gzl-cell_spreadcon");
        var siblings = $(this).parent().siblings();
        if (con.is(":hidden")) {
          $(this).addClass("show");
          siblings.find(".gzl-cell_spread").removeClass("show");
          siblings.find(".gzl-cell_spreadcon").slideUp(200);
          con.slideDown(200);
        }else{
          $(this).removeClass("show");
          con.slideUp(200);
        }
      });
      // 模拟层模拟到签证人员选择
      $(".visaMaterials").find(".gzl-flex__item").on("click",function(){
        simulateMenber();
      })
      // 本页前进后退;
      $.popstate(function () {
        if(window.location.hash == "#1"){
          return
        }else{
            if ($(".visaMaterials").length < 1){
              window.history.go(-1);
            }else{
              $(".visaMaterials").remove();
            }
        }
      })
    }
    // 显示模板
    $.simulatePagePopup({
      wrapperClass: "visaMaterials",
      content: siumulateContent,
      hasFooter : true, //是否有底部
      footerContent: siumulateFooter,
      bindTplEvent: templateEvents,
      pushHistoryStr: '#1'
    });
  }

  // 签证人员选择
  function simulateMenber(){
    var menbersSel = { list: ["学龄前儿童","自由职业者","退休人员","青年","家庭主妇","在职人员"] };
        // 定义模板
        var menberContent = _.template($("#simulate-visa-menbers").html())(menbersSel);
        var menberFooter = '<footer class="gzl-footer">'+
        '            <div class="gzl-footbar visaMenberSelAll travel-dt-footbar">'+
        '              <div class="gzl-footbar__bd">'+
        '                <div class="contact-check">'+
        '                      <svg class="icon" aria-hidden="true">'+
        '                        <use xlink:href="#icon-uncheck"></use>'+
        '                      </svg><span>全选</span>'+
        '                </div>'+
        '              </div>'+
        '              <div class="gzl-flex__item">'+
        '                <div class="btn"><a href="javascript:void(0)">发送邮箱</a></div>'+
        '              </div>'+
        '            </div>'+
        '          </footer>';

        function menbersEvent(){
          //  默认点击事件
          function iconDefault(el){
            var icon = el.find("use");
            var checkStatus = icon.attr("xlink:href");
            if(checkStatus !== "#icon-checked"){
              if (checkStatus === "#icon-uncheck") {
                icon.attr("xlink:href","#icon-check").parent().addClass("checked");
              }else if (checkStatus === "#icon-check"){
                icon.attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
              }
            }
          }
          // 所有长度
          function judgeAll(){
            var selectItem = $(".visaMenberSel .gzl-section").find(".contact-check").length;
            var checkItem = $(".visaMenberSel .gzl-section").find(".checked").length;
            var icon = $(".visaMenberSel").find(".gzl-footbar__bd").find("use");
            if(selectItem == checkItem){
              icon.attr("xlink:href","#icon-check").parent().addClass("checked");
            }else{
              icon.attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
            }
          }
          // 点击事件
          $(".visaMenberSel").find(".gzl-cell").on("click",function () {
            iconDefault($(this).find(".contact-check"));
            judgeAll();
          })
          // 全选点击事件
          $(".visaMenberSel").find(".gzl-footbar__bd").on('click',function(){
            var self = $(this);
            iconDefault(self);
            if(self.find(".checked").length){
              var icon =$(".visaMenberSel .gzl-section").find(".contact-check").find("use");
              icon.attr("xlink:href","#icon-check").parent().addClass("checked");
            }else{
              var icon = $(".visaMenberSel .gzl-section").find(".contact-check").find("use");
              icon.attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
            }
            
          })

          $(".visaMenberSel .visaMenberSelAll").find(".gzl-flex__item").on("click",function(){
            $.modal({
              title: "发送至邮箱",
              text: "<div class=sendEmailPopUp><input placeholder=请输入邮箱></div><div class=sendEmailError>输入的邮箱有误,请重新输入</div>",
              buttons: [
                {text: "发送", className: "sendEmail", onClick: function () {
                  $.toast({
                    icon: '#icon-toast-tick',
                    text: '发送成功',
                    boxClass: 'toast-tick'
                  });
                }},
              ]
            });
          })
        }

        // 本页前进后退;
      $.popstate(function () {
        if ($(".visaMenberSel").length < 1){
          window.history.go(-1);
        }else{
          $(".visaMenberSel").remove();
        }
      })

        // 选择签证人数
        $.simulatePagePopup({
          wrapperClass: "visaMenberSel",
          content: menberContent,
          hasFooter : true, //是否有底部
          footerContent: menberFooter,
          bindTplEvent: menbersEvent,
          pushHistoryStr: '#2'
        });
  }


  $(".gzl-asidebar .to-top").on("click", function () {
    $(".gzl-section").toTop();
  })

})
